//
//  ShipEntity.m
//  ShootEmUp
//
//  Created by Steffen Itterheim on 18.08.10.
//  Copyright 2010 Steffen Itterheim. All rights reserved.
//

#import "ShipEntity.h"
#import "CCAnimationHelper.h"
#import "GameScene.h"


@interface ShipEntity (PrivateMethods)
-(id) initWithShipImage;
@end

@implementation ShipEntity

+(id) ship
{
	return [[[self alloc] initWithShipImage] autorelease];
}

#ifdef GAME_NEW
-(id) initWithShipImage
{
	// Loading the Ship's sprite using a sprite frame name (eg the filename)
    
    
    NSString * str_pic_name=nil;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        str_pic_name=@"bird_pebbles_preview-hd.png";
    }
    else{
        str_pic_name=@"bird_pebbles_preview.png";
    }
    
    
	if ((self = [super initWithFile:str_pic_name]))
	{
		/*
		// create an animation object from all the sprite animation frames
		CCAnimation* anim = [CCAnimation animationWithFrame:@"ship-anim" frameCount:5 delay:0.08f];
		
		// add the animation to the sprite (optional)
		//[sprite addAnimation:anim];
		
		// run the animation by using the CCAnimate action
		CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
		CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
		[self runAction:repeat];
		 */
		
		//[self birdPowerAnimation];
		
		frameCount=0;
		bChangeFrame=false;
		
		[self initBirdFrames];
	}
	return self;
}
#else
fasfasfasf
-(id) initWithShipImage
{
	// Loading the Ship's sprite using a sprite frame name (eg the filename)
	if ((self = [super initWithSpriteFrameName:@"ship.png"]))
	{
		// create an animation object from all the sprite animation frames
		CCAnimation* anim = [CCAnimation animationWithFrame:@"ship-anim" frameCount:5 delay:0.08f];
		
		// add the animation to the sprite (optional)
		//[sprite addAnimation:anim];
		
		// run the animation by using the CCAnimate action
		CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
		CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
		[self runAction:repeat];
	}
	return self;
}
#endif//

// moved back to ShipEntity ... the enemies currently don't need it and it gets in the way when
// resetting enemy positions during spawn

-(void)dealloc
{
	[frameArray release];
	[super dealloc];
}

-(void)initBirdFrames
{
	frameArray = [[NSMutableArray alloc] init];
	
	/////////////////////////////////////////////煽动翅膀的鸟
	NSString *fish_name=[NSString stringWithFormat:@"bird_pebbles"];
    
    
    NSString * fish_name_plist=nil;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        fish_name_plist=@"gameEngineSpriteSheetDefault-hd.plist";
    }
    else{
        fish_name_plist=@"gameEngineSpriteSheetDefault.plist";
    }
    

	
	// Load the Texture Atlas sprite frames, this also loads the Texture with the same name.
	CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
	[frameCache addSpriteFramesWithFile:fish_name_plist];
	
	for (int i=0; i<MAX_FRAME_COUNT; i++) {
		NSString* file = [NSString stringWithFormat:@"%@%i.png", fish_name, i];
		//CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
		CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
		[frameArray addObject:frame];
	}

	assert([frameArray count]==MAX_FRAME_COUNT);
}


// override setPosition to keep entitiy within screen bounds
-(void) setPosition:(CGPoint)pos
{
	// If the current position is (still) outside the screen no adjustments should be made!
	// This allows entities to move into the screen from outside.
    
	if (CGRectContainsRect([GameScene screenRect], [self boundingBox]))
	{
		CGSize screenSize = [[CCDirector sharedDirector] winSize];
		float halfWidth = self.contentSize.width * 0.5f;
		float halfHeight = self.contentSize.height * 0.5f;
		
		// Cap the position so the Ship's sprite stays on the screen
		if (pos.x < halfWidth)
		{
			pos.x = halfWidth;
		}
		else if (pos.x > (screenSize.width - halfWidth))
		{
			pos.x = screenSize.width - halfWidth;
		}
		
		if (pos.y < halfHeight)
		{
			pos.y = halfHeight;
		}
		else if (pos.y > (screenSize.height - halfHeight))
		{
			pos.y = screenSize.height - halfHeight;
		}
	}
	
	[super setPosition:pos];
}


/*
 垂直方向移动
 */
-(void) runMoveByActionV:(int)yDelta
{
	
	CCMoveBy * move=[CCMoveBy actionWithDuration:0.1 position:ccp(0,yDelta)];
	[self runAction:move];
	
	return;
	
	/////////////////////////////////////////////煽动翅膀的鸟
	NSString *fish_name=[NSString stringWithFormat:@"bird_pebbles"];
    
    NSString * fish_name_plist=nil;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        fish_name_plist=@"gameEngineSpriteSheetDefault-hd.plist";
    }
    else{
        fish_name_plist=@"gameEngineSpriteSheetDefault.plist";
    }
    
	//NSString * fish_name_plist=[NSString stringWithFormat:@"gameEngineSpriteSheetDefault.plist"];
	
	// Load the Texture Atlas sprite frames, this also loads the Texture with the same name.
	CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
	[frameCache addSpriteFramesWithFile:fish_name_plist];
	
	// create an animation object from all the sprite animation frames
	CCAnimation* anim=[CCAnimation animationWithFrame:fish_name  frameCount:MAX_FRAME_COUNT delay:0.5f];
	
	// add the animation to the sprite (optional)
	//[self addAnimation:anim];
	
	
	
	float time=0.5;
	id action = [CCAnimate actionWithDuration:time animation:anim restoreOriginalFrame:NO];
	
	////test
	//target.position=ccp(100,100);
	
	
	//[target runAction:action];
	
	//我忘了旋转是哪个 用spawn就可以做到，我这个是又跑动又往前走。
	
	
	// 执行劢画效果 
	//id actionAnim = [CCAnimate actionWithDuration:0.1 animation:anim restoreOriginalFrame:NO];
	
	//id ac1=[CCRepeatForever actionWithAction:action];
	id ac2=[CCRepeat actionWithAction:action times:0xFFFFFFFF];
	
	
	/*带对象参数
	 */
	/*
	 可以 
	 action=[Sequence actions:[Spawn actions:
	 [MoveTo actionWithDuration:1.5+i*0.1 position:CGPointMake(371,30)],
	 [Repeat actionWithAction:[Animate actionWithAnimation: ani] times:3],
	 nil],
	 [FadeTo actionWithDuration:0 opacity:0]];
	 [people runAction:action];
	 我忘了旋转是哪个 用spawn就可以做到，我这个是又跑动又往前走。
	 */
	
	
	
	///// 用spawn就可以做到，我这个是又跑动又往前走
	id together;
	if (yDelta>0) {
		together==[CCSpawn actions:ac2,move,nil];/////ac2, ,要在move里面做回调，因为动画是一直走的，而移动是会结束的，然后调用消失函数
	}
	else {
		together==[CCSpawn actions:move,nil];
	}

	
	id seq;
	seq= [CCSequence actions:together,nil];
	
	
	[self runAction:seq];
	////////////////////////
	
}

-(void)startBirdFrame
{
	bChangeFrame=true;
}

-(void)stopBirdFrame
{
	bChangeFrame=FALSE;
	
	/////////////////////////////////////////////煽动翅膀的鸟
	NSString *fish_name=[NSString stringWithFormat:@"bird_pebbles"];
    
    NSString * fish_name_plist=nil;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        fish_name_plist=@"gameEngineSpriteSheetDefault-hd.plist";
    }
    else{
        fish_name_plist=@"gameEngineSpriteSheetDefault.plist";
    }
	//NSString * fish_name_plist=[NSString stringWithFormat:@"gameEngineSpriteSheetDefault.plist"];
	
	// Load the Texture Atlas sprite frames, this also loads the Texture with the same name.
	CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
	[frameCache addSpriteFramesWithFile:fish_name_plist];
	
	NSString* file = [NSString stringWithFormat:@"%@%i.png", fish_name, 1];  //让鸟的翅膀留在最高处
	//CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
	CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
	
	[self setDisplayFrame:frame];
}

-(void)changeBirdFrame
{
	if (!bChangeFrame) 
    {
		return;
	}
		
	

	CCLOG(@"frameCount=%d\n",frameCount);
	
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (frameCount>=MAX_FRAME_COUNT_IPAD) {
            frameCount=0;
        }
    }
    else{
       	if (frameCount>=MAX_FRAME_COUNT) {
            frameCount=0;
        }
    }
    
    

	

    
	
	//[self.texture release];
	//self.texture = [[CCTextureMgr sharedTextureMgr] addImage: @"plan4.png"];
	
	CCSpriteFrame* frame=[frameArray objectAtIndex:frameCount];
	[self setDisplayFrame:frame];
	
	frameCount++;
}

-(void)birdPowerAnimation
{

	
	///test code 
	/*
	 CCSprite * tmp=[CCSprite spriteWithFile:@"bird_pebbles_preview.png"];
	 tmp.position=ccp(100,100);
	 [self addChild:tmp];
	 */
	
	/////////////////////////////////////////////煽动翅膀的鸟
	NSString *fish_name=[NSString stringWithFormat:@"bird_pebbles"];
    
    NSString * fish_name_plist=nil;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        fish_name_plist=@"gameEngineSpriteSheetDefault-hd.plist";
    }
    else{
        fish_name_plist=@"gameEngineSpriteSheetDefault.plist";
    }
	//NSString * fish_name_plist=[NSString stringWithFormat:@"gameEngineSpriteSheetDefault.plist"];
	
	// Load the Texture Atlas sprite frames, this also loads the Texture with the same name.
	CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
	[frameCache addSpriteFramesWithFile:fish_name_plist];
	
	// create an animation object from all the sprite animation frames
	CCAnimation* anim=[CCAnimation animationWithFrame:fish_name  frameCount:MAX_FRAME_COUNT delay:0.5f];
	
	// add the animation to the sprite (optional)
	//[self addAnimation:anim];
	
	
	
	float time=0.5;
	id action = [CCAnimate actionWithDuration:time animation:anim restoreOriginalFrame:NO];
	
	////test
	//target.position=ccp(100,100);
	
	
	//[target runAction:action];
	
	//我忘了旋转是哪个 用spawn就可以做到，我这个是又跑动又往前走。
	
	
	// 执行劢画效果 
	//id actionAnim = [CCAnimate actionWithDuration:0.1 animation:anim restoreOriginalFrame:NO];
	
	//id ac1=[CCRepeatForever actionWithAction:action];
	id ac2=[CCRepeat actionWithAction:action times:0xFFFFFFFF];
	
	
	/*带对象参数
	 */
	/*
	 可以 
	 action=[Sequence actions:[Spawn actions:
	 [MoveTo actionWithDuration:1.5+i*0.1 position:CGPointMake(371,30)],
	 [Repeat actionWithAction:[Animate actionWithAnimation: ani] times:3],
	 nil],
	 [FadeTo actionWithDuration:0 opacity:0]];
	 [people runAction:action];
	 我忘了旋转是哪个 用spawn就可以做到，我这个是又跑动又往前走。
	 */
	
	
	
	///// 用spawn就可以做到，我这个是又跑动又往前走
	//id together=[CCSpawn actions:ac2,move,nil];/////ac2, ,要在move里面做回调，因为动画是一直走的，而移动是会结束的，然后调用消失函数
	
	id seq;
	seq= [CCSequence actions:ac2,nil];
	
	
	[self runAction:seq];
	////////////////////////
	
}

@end
